package com.example.helloworld2

import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.ComponentActivity

class MainActivity : ComponentActivity() {

    // 当前索引
    private var currentIndex = 0

    // 多个国家的数据
    private val countries = listOf(
        Country(R.drawable.flag_china, R.string.hello_Chinese),
        Country(R.drawable.flag_us, R.string.hello_English),
        Country(R.drawable.flag_gem, R.string.hello_German),


        )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val textView = findViewById<TextView>(R.id.textView)
        val button = findViewById<Button>(R.id.button)
        val imageView = findViewById<ImageView>(R.id.imageView2)

        // 初始化显示第一个国家
        updateUI(textView, imageView, button)

        button.setOnClickListener {
            // 每点击一次，切换到下一个国家
            currentIndex = (currentIndex + 1) % countries.size
            updateUI(textView, imageView, button)
        }
    }

    private fun updateUI(textView: TextView, imageView: ImageView, button: Button) {
        val country = countries[currentIndex]
        textView.text = getString(country.textRes)
        imageView.setImageResource(country.flagRes)

    }

    // 数据类：存放国旗和对应文字资源
    data class Country(val flagRes: Int, val textRes: Int)
}
